{include file="public_head"}
<script src="__STATIC__/js/operation.js"></script>
{include file="public_menu"}
<div class="content_cart">
    {empty name="data"}
    <div class="empty_cart">
        <div>
            <img src="__STATIC__/img/icon62.png" class="left">
            <div class="tips left">
                购物车空空的哦~，去看看心仪的商品吧~
                <a href="{:url('Index/index')}">去购物></a>
            </div>
        </div>
    </div>
    {else/}
    <div class="main">
        <ul class="title">
            <li class="all"><input type="checkbox" class="checkbox" id="checkAll"><label for="checkAll">全选</label></li>
            <li class="price text-center">单价</li>
            <li class="num text-center">数量</li>
            <li class="total text-center">小计</li>
        </ul>

        {volist name="data" id="v" key='i'}
        <div class="shop">
            <div class="left">
                <input type="checkbox"  class="radio ckAll_{$v[0]->shopGoods->member_id} check_box" id="choose_{$i}" dataType="shop_title">
                <!--<input type="checkbox"  class="radio ckAll_{$v[0]->shopGoods->member_id} check_box" id="choose_{$i}" onclick="checkBox({$v[0]->shopGoods->member_id})" dataType="shop_title">-->
                <label for="choose_{$i}">
                    {$key}<a href="{:url('Shop/index',['shop_id'=>$v[0]->shopGoods->member_id])}"><img src="__STATIC__/img/icon28.png"></a>
                </label>
            </div>
        </div>
        <div class="shop_det" >
            {volist name="$v" id="item" key="k"}
            <ul class="list" id="goods_details_{$item.id}">
                <li class="choose">
                    <input type="hidden" name="new_price[]" id="p_{$item.id}" value="{$item['good_total']}" />
                    <!--<input type="checkbox" class="radio ck_{$item.shopGoods.member_id} check_box goods_check" id="choose_{$item.id}" value="{$item.id}" onclick="settlement_goods({$item.shopGoods.member_id});" dataType="shop_goods">-->
                    <input type="checkbox" class="radio ck_{$item.shopGoods.member_id} check_box goods_check" id="choose_{$item.id}" value="{$item.id}" dataType="shop_goods">
                    <label for="choose_{$item.id}" class="label"> </label>
                    <input type="hidden" name="new_num[]" id="b_{$item.id}" value="{$item.number}" />
                </li>

                <li class="goods">
                    <a href="{:url('ShopGoods/index',['good_id'=>$item.shopGoods.id])}">
                        <div class="goods_det">
                            <img src="/{$item.shopGoods.thumb_img}">
                            <div>
                                <div class="name">{$item.shopGoods.title}</div>
                                <!--<div class="tips">支持7天无理由退货</div>-->
                            </div>
                        </div>
                    </a>
                </li>
                <li class="size text-left">{if condition="$item.attr"}{$item.attr}{else/}无属性{/if}</li>
                <li class="price text-center">￥<span id="price_{$item.id}">{$item.price}</span></li>
                <li class="num text-center">
                    <div class="text-center">
                        <ul>
                            <li onclick="minus({$k},{$item.id})" style="cursor:pointer;">-</li>
                            <li><input type="text" id="good_num_{$item.id}" class="_num_{$item.id} input_num" onkeyup="num_control(this,{$item.id},{$item.shopGoods.inventory})" value="{$item.number}" /></li>
                            <li onclick="plus({$k},{$item.shopGoods.id},{$item.id})" style="cursor:pointer;">+</li>
                        </ul>
                    </div>
                    {if condition="$item.shopGoods.inventory lt 0"}
                    <span>无货</span>
                    {else/}
                    <span>有货</span>
                    {/if}
                </li>
                <li class="total text-center"><span style="display: inline;">￥</span><span id="good_total_{$item.id}" style="display: inline;">{$item['good_total']}</span></li>
            </ul>
            {/volist}
        </div>
        {/volist}



        <div class="settle">
            <div class="left">
                <span><a href="javascript:delete_all();">删除选中的产品</a></span>
            </div>
            <div class="right">
                <div>
                    <span>已选择<span id="settlement">0 </span>件商品 </span>
                    <p>总价：<span >￥<span id="zong_jia">0.00</span></span></p>
                </div>
                <input type="submit" value="结算" onclick="settle()">
            </div>
        </div>
    </div>
    {/empty}
</div>
<script>
    //查询商品是否为限购商品
    function limit(d) {
        var c = 0;
        $.ajax({
            url: "query_limited",
            data: {'id': d},
            dataType: 'json',
            async: false,
            type: 'post',
            success: function (t) {
                c = t;
            }
        });
        return c;
    }

    //数量增加   o元素位置 b商品id d购物车id
    function plus(o, b, d) {
        var a = $('#good_num_'+d);
        var c = limit(d);
        var e = $('#b_'+d);
        if (c == 1) {
            layer.msg('限购商品,无法增加数量');
            return false
        }
        //服务器查询商品库存
        $.ajax({
            url: "query_inventory",
            data: {'id': b, 'num': parseInt(a.val()), 'cart_id': d},
            dataType: 'json',
            async: false,
            type: 'post',
            success: function (t) {
                if (t.code == 200) {
                    a.val(parseInt(a.val()) + 1);  //增加显示的值
                    e.val(parseInt(e.val()) + 1);   //增加提交的藏域的值
                    chage_price(d);
                    chage_total();
                    settlement_goods();
                } else {
                    layer.msg(t.message);
                    return false;
                }
            }
        });

    }

    // 商品数量减少 o元素位置 d商品的ID
    function minus(o,d) {
        var a = $('#good_num_'+d);
        var e = $('#b_'+d);
        if (parseInt(a.val()) <= 1) {
            layer.msg('不能再减了');
            return false
        }

        var num = parseInt(a.val()) - 1;
        $.ajax({
            url:"query_reduce",
            data:{'cart_id':d},
            dataType: 'json',
            async: false,
            type: 'post',
            success:function (t) {
                if (t.code == 200) {
                    a.val(num);   //改变显示量
                    e.val(num);   //改变提交的隐藏域
                    chage_price(d);
                    chage_total();
                    settlement_goods();
                }
            }
        });

    }

    function selectAll() {
//点击店铺是判断全选是是否选中
        var one = "true";
        $('.shop input[dataType="shop_title"]').each(function () {
            if ($(this).is(':checked')){
//                one = "true";
            }else{
                one = "false";
            }
        });
        if (one=="true"){
            $('#checkAll').prop("checked",true);
        }else{
            $('#checkAll').removeAttr("checked",false);
        }
        settlement_goods();
        chage_total();

    }
    $(function () {
        //点击全选
        $('#checkAll').click(function(){
            if($(this).is(':checked')){
                $('input[type="checkbox"]').each(function(){
                    //此处如果用attr，会出现第三次失效的情况
                    $(this).prop("checked",true);
                });
            }else{
                $('input[type="checkbox"]').each(function(){
                    $(this).removeAttr("checked",false);
                });
            }
            settlement_goods();
            chage_total();
        });


        //点击店铺
        $('.shop input[dataType="shop_title"]').click(function(){
            if($(this).is(':checked')){
                $(this).parents('.shop').next(".shop_det").find('.choose input[dataType="shop_goods"]').each(function(){
                    //此处如果用attr，会出现第三次失效的情况
                    $(this).prop("checked",true);
                });
            }else{
                $(this).parents('.shop').next(".shop_det").find('.choose input[dataType="shop_goods"]').each(function(){
                    $(this).removeAttr("checked",false);
                });
            }
            selectAll();
        });

        //点击商品
        $('.choose input[dataType="shop_goods"]').click(function(){
            if($(this).is(':checked')){
                $(this).prop("checked",true);
            }else{
                $(this).removeAttr("checked",false);
            }
            //当点击商品时判断店铺是否选中 全选是否选中
            var two ="true";
            $(this).parents('.shop_det').find('.choose input[dataType="shop_goods"]').each(function () {
                if ($(this).is(':checked')){
                }else{
                    two = "false";
                }
            });
            if (two=="true"){
                $($(this).parents('.shop_det').prev(".shop").find('input[dataType="shop_title"]')).prop("checked",true);
            }else{
                $($(this).parents('.shop_det').prev(".shop").find('input[dataType="shop_title"]')).removeAttr("checked",false);
            }
            selectAll();
        });

    })

    // 结算商品数量
    function settlement_goods(){

        var total = 0;
        $('.goods_check:checked').each(function (i,o) {
            var mm = parseFloat($(o).next().next().val());
            total+= mm;
        });

        $('#settlement').text(total);
        chage_total();

    }

    //  小计价格变化
    function chage_price(d) {
        var price = parseFloat($('#price_'+d).text());
        var num = parseInt($('#good_num_'+d).val());
        var total  = changeTwoDecimal_f(accMul(price,num));
        $('#good_total_'+d).text(total);
        $('#p_'+d).val(total);
    }

    // 总价格变化
    function chage_total() {
        var total = 0;
        $('.goods_check:checked').each(function (i,o) {
            var mm = parseFloat($(o).prev().val());
            total+= mm;
        });

        $('#zong_jia').text(changeTwoDecimal_f(total));
    }

    //去结算
    function settle(){
        if (parseInt($('#settlement').text()) > 0){
            //页面选中的商品id
            var cart_id_str = '';
            $('.goods_check:checked').each(function (i,o) {
                if (cart_id_str == ''){
                    cart_id_str = $(o).val();
                }else{
                    cart_id_str += ','+$(o).val();
                }
            });

            //跳转到确认订单那里
            if (cart_id_str != ''){
                window.location.href = "{:url('shop/shop_cart/confirm_order')}?cart_id="+cart_id_str;
            }

        }else{
            layer.msg('没有商品可去结算');
        };
    }

    // 移除商品
    function remove_good(id) {
        $.ajax({
            url:'cart_del',
            dataType: 'json',
            type: 'post',
            data:{id:id},
            success:function (t) {
                if (t.code==200) {
                    layer.msg('移除成功！');
                    location.reload();
                }else {
                    layer.msg('移除失败！');
                }
            }
        })
    }

    // 手动输入数量
    function num_control(o,n,c){
        if(!/^\d+$/.test(o.value)) {
            o.value=o.value.replace(/[^\d]+/g,'');
            $('#good_num_'+n).val(1);
            layer.msg('格式错误');
            return false;
        }
        if (o.value == ''){
            setTimeout(function () {
                if (o.value == '' || o.value === 0){
                    o.value = n;
                    $('#good_num_'+n).val(1);
                }
            },1000);
        }else if(o.value >= 99){
            o.value = 99;
            $('#good_num_'+n).val(99);
        }else if(o.value < 1){
            o.value = 1;
            $('#good_num_'+n).val(1);
            layer.msg('数量至少为 1');
            return false;
        }

        if (o.value>c){
            layer.msg("库存不足！！");
        }
        $('#b_'+n).val(o.value);
        chage_price_s(n);
        chage_total();
    }


    function chage_price_s(d) {

        var price = parseFloat($('#price_'+d).text());
        var num = parseInt($('#good_num_'+d).val());
        var total  = changeTwoDecimal_f(accMul(price,num));
        $.ajax({
            url:"charge_number",
            data:{'id':d,'number':num},
            dataType: 'json',
            async: false,
            type: 'post',
            success:function (t) {
                if (t.code==200) {
                    $('#good_total_'+d).text(total);
                    $('#p_'+d).val(total);
                }
            }
        });

    }

    // 购物车清空多个商品
    function delete_all() {
        //页面选中的商品id
        var cart_id_str = '';
        $('.goods_check:checked').each(function (i,o) {
            if (cart_id_str == ''){
                cart_id_str = $(o).val();
            }else{
                cart_id_str += ','+$(o).val();
            }
        });
        if (cart_id_str=='') {
            layer.msg('请选择商品！');
        }else {
            $.ajax({
                url:"{:url('shop/shop_cart/delete_all')}",
                type:'post',
                dateType:'json',
                data:{'ids':cart_id_str},
                success:function (data) {
                    if (data.code==200) {
                        layer.msg('移除成功！');
                        location.reload();
                    }else {
                        layer.msg('网络繁忙！');
                    }
                }
            });
        }
    }
</script>
{include file="public_foot"}